#!/bin/bash
#这里可替换为你自己的执行程序，其他代码无需更改
APP_NAME=tailf-web-1.2.1-SNAPSHOT.jar
source /etc/profile
BUILD_ID=dontKillMe
ls_date=`date +%Y%m%d`

#启动方法
start(){
        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
        if [ "$pid" ]; then
                echo "$APP_NAME is already running. pid=$pid ."
        else    
                if [ -f $APP_NAME ];then            		
		    nohup ${JAVA_HOME}/bin/java -jar /root/tailf/tailf-web-1.2.1-SNAPSHOT.jar --tailf.monitor-path=/root/tailf >> lg_$ls_date.log  2>&1 &
                    sleep 10
                    echo "$APP_NAME now is running"
                else
                    echo '$APP_NAME MISSING'
                fi
        fi
}

#停止方法
stop(){
        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
        if [ "$pid" ]; then
                kill -15 $pid
                echo "Pid:$pid stopped"
        else
                echo "$APP_NAME is not running"
        fi

        case "$1" in
            rn)		
	        if [ -f $APP_NAME ];then
	           mv tailf-web-1.2.1-SNAPSHOT.jar tailf-web-1.2.1-SNAPSHOT-`date +"%Y-%m-%d_%H:%M:%S"`.jar
	        fi
                ;;

        esac
}

#输出运行状态
status(){
        pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
        if [ "$pid" ]; then
                echo "$APP_NAME is running. Pid is ${pid}"
        else
                echo "$APP_NAME is NOT running."
        fi
}

#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
        up)
                start
                ;;
        down)
                stop rn
                ;;
        status)
                status
                ;;
        restart)
                stop
                sleep 5
                start
                ;;
        *)
                echo "Usage:{up|down|status|restart}"
                ;;

esac
exit 0
